package servlet;

import dao.UsersDao;
import dao.impl.UsersDaoImpl;
import utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserPasswordServlet", urlPatterns = "/bigProjects/UserPasswordServlet")

public class UserPasswordServlet extends HttpServlet {

    //修改密码
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //说是使用多态,提高耦合度
        UsersDao usersDao = new UsersDaoImpl();
        // 设置响应的内容类型为 JSON
        resp.setContentType("application/json");
        //设置ContentType字段值
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        Integer id = Integer.valueOf(req.getParameter("id"));
        boolean b = usersDao.changePassword(oldPassword, newPassword, id);
        if (b) resp.getWriter().write(ResponseResult.success().toString());
        else resp.getWriter().write(ResponseResult.failure("修改失败").toString());
    }
}
